/**
 * Source file: CheckPostal.ejs
 * Copyright (C) 2013 : Quantum Solutions
 * Id: $Id$
 * Date: $Date$
 */

 
//Turn on your selected Environment before exporting your form from TeleForm.
// serverName is also defined in ConnectWebService (should be the same value)
//var serverName = "http://127.0.0.1:50057"; // rob VS
//var serverName = "http://WIN-OV9FIKDJ4BV:88"; // Willie test
//var serverName = "http://suglifeabiqa01:88"; // Nedbank test ACBVAL
//var serverName2 = "http://suglifeabiqa01:89"; // NedBank test Suburb
var serverName = "Http://2008-4.quantumsolutions.co.za:91"; // Quantum Cloud Test acbval Autocompletes
var serverName2 = "http://2008-4.quantumsolutions.co.za:92";// Quantum Cloud Test Suburb AutoCompletes 

 function CheckPostal(suburbField,codeField) { 
		var suburb = EFormGetValue(suburbField);
		var code = EFormGetValue(codeField);
		
		//alert("suburb="+suburb+" code="+code+" suburbField"+suburbField);
		if (code == "") return true;

        var parameters = '{"suburb":"' + suburb + '",'
        + '"code":"' + code + '"'
        +'}';

        $.ajax({
            url: serverName2+"/ACBServiceProxy.svc/PostalCheck",
            type: 'POST',
            async: false,
            dataType: 'json',
            contentType: 'application/json; charset="utf-8"',
            data: parameters,
            complete: function (response) {
                //alert('complete: response ' + response + ' responseText ' + response.responseText);
                var res = jQuery.parseJSON(response.responseText);
				if (res.PostalCheckResult != "") {
					alert("Invalid Suburb, should be one of the following:" + res.PostalCheckResult);
					EFormSetValue(codeField,"");
					EFormSetFocus(EFormGetField(suburbField));  				
				}
            },
            error: function (response) {
                alert('error ' + response.responseText);
            }
        });
        return true;
    }